# Author: Mr.Xu
# @Time : 2019/12/17 20:06
import datetime
import json
import os,random

if __name__ == '__main__':
    import django

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "videoapi.settings.dev")
    django.setup()
    from home import models as home_models
    from video import models as video_models
    from user import models as user_models

    '''过期时间'''
    # obj = user_models.User.objects.filter(pk=14).first()
    # if obj.vip_type != 0:
    #     if  datetime.datetime.now() > obj.exp_date:
    #         print(True)
    #     else:
    #         print(False)
    # else:
    #     print(123123)
    # print(obj.exp_date, type(obj.exp_date))

    # video_obj = video_models.Video.objects.filter(pk=3)
    # print(video_obj.values_list("video_section__v_id").order_by("-video_section__order"))

    # obj = models.Video.objects.filter(pk=3).values_list("video_section__v_id").order_by("-video_section__order")
    # obj = models.Video_Credits.objects.filter(video_id=3,role_id=1).all().values_list("credits__name")
    # print(list(obj))

    # print(datetime.date.today()+datetime.timedelta(30))

    data = {'play_time': 52.719502, 'section_id': 69}
    video_models.Record.objects.create(
        user_id=14,
        video_section_id=data.get("section_id"),
        play_time= data.get("play_time")
    )
    # if 52.719502 > 52:
        # print(123123)

    '''剧集封面图片'''
    # query_set = video_models.Video_Section.objects.all()
    # for obj in query_set:
    #     obj.image = obj.link+"?vframe/jpg/offset/1"
    #     obj.save()

    '''推荐表数据'''
    # home_models.Recommend.objects.all().delete()
    # obj_set = []
    # cat_list = video_models.Category.objects.all()
    # cat_list = list(cat_list)
    # # print(cat_list)
    # for cat in cat_list:
    #     obj_list = video_models.Video.objects.filter(category__pk=cat.pk).all().order_by("-heat")[:5]
    #     # print(cat.pk)
    #     i=5
    #     for obj in obj_list:
    #         # print(i)
    #         if obj.sections == obj.pub_sections:
    #             if obj.category.name == "电影":
    #                 status =  "正片"
    #             elif obj.category.name == "电视剧":
    #                 status = f"{obj.sections}集全"
    #             elif obj.category.name == "综艺":
    #                 status =  f"{obj.sections}期全"
    #             elif obj.category.name == "动漫":
    #                 status =  f"{obj.sections}话全"
    #             elif obj.category.name == "纪录片":
    #                 status =  f"{obj.sections}集全"
    #             else:
    #                 status =  f"{obj.sections}集全"
    #         else:
    #             if obj.category.name == "电视剧":
    #                 status =  f"更新至{obj.pub_sections}集"
    #             elif obj.category.name == "综艺":
    #                 status =  f"更新至{obj.pub_sections}期"
    #             elif obj.category.name == "动漫":
    #                 status =  f"更新至{obj.pub_sections}话"
    #             elif obj.category.name == "纪录片":
    #                 status =  f"更新至{obj.pub_sections}集"
    #             else:
    #                 status =  f"更新至{obj.pub_sections}集"
    #         obj_set.append(home_models.Recommend(
    #             title=obj.title,
    #             score=obj.score,
    #             image=obj.image,
    #             heat=obj.heat,
    #             order = i,
    #             is_free=obj.is_free,
    #             category_id=cat.pk,
    #             video_id=obj.pk,
    #             status=status,
    #         ))
    #         i -= 1
    # home_models.Recommend.objects.bulk_create(obj_set)

    '''轮播图表数据'''
    # obj_list = video_models.Video.objects.all().order_by("-order")[:4]
    # i=4
    # s=1
    # for obj in obj_list:
    #     video_id = obj.pk
    #     image_url = f"banner/{i}.jpg"
    #     print(image_url)
    #     home_models.Banner.objects.create(
    #         title=obj.title,
    #         link=f"/video/section/{video_id}",
    #         image= image_url,
    #         order=s,
    #     )
    #     i-=1
    #     s+=1

    '''修改视频链接'''
    # with open(f"../vedioapi/media/处理后的豆瓣电影视频链接.json", "r", encoding="utf8") as f1:
    #     url_dic = json.load(f1)
    #     url_list = url_dic["data"]
    # for i in range(len(url_list)):
    #     url = url_list[i]
    #     if url == "http://q233bpn44.bkt.clouddn.com/1.mp4":
    #         url = "http://q233bpn44.bkt.clouddn.com/common.mp4"
    #     else:
    #         url = f"http://q233bpn44.bkt.clouddn.com/{i+1}.mp4"
    #     obj = models.Video_Section.objects.filter(pk=i+1).update(link=url)
    #     print(i)

    '''修改图片'''
    # for i in range(64):
    #     obj = models.Video.objects.filter(pk=i+1).update(image=f"video/{i}.jpg")

    '''插入视频剧集数据'''
    # with open(f"../vedioapi/media/处理后的豆瓣电影视频链接.json", "r", encoding="utf8") as f1, \
    #         open(f"../vedioapi/media/处理后的豆瓣电影数据.json", "r", encoding="utf8") as f2:
    #     url_dic = json.load(f1)
    #     url_list = url_dic["data"]
    #     data_dic = json.load(f2)
    #     data_list = data_dic["data"]
    #
    # obj_list = []
    # for i in range(len(url_list)):
    #     desc = data_list[i].get("description")
    #     title = data_list[i].get("title")
    #     datePublished = data_list[i].get("datePublished").split("-")
    #     pub_time = datetime.date(
    #         year=int(datePublished[0]),
    #         month=int(datePublished[1]),
    #         day=int(datePublished[2]))
    #     link = url_list[i]
    #     video_id = i+1
    #
    #     res = models.Video_Section.objects.filter(video_id=i+1).values_list()
    #     if res:
    #         res = len(res) + 1
    #     else:
    #         res = 1
    #
    #     obj = models.Video_Section(
    #         name = title,
    #         desc = desc,
    #         pub_time = pub_time,
    #         link = link,
    #         order = res,
    #         video_id = video_id,
    #     )
    #     obj_list.append(obj)
    # models.Video_Section.objects.bulk_create(obj_list)


    '''插入视频数据'''
    # Type_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    # with open(f"../vedioapi/media/处理后的豆瓣电影数据.json", "r", encoding="utf8") as f:
    #     data_dic = json.load(f)
    #     data_list = data_dic["data"]
    #
    # i=0
    # obj_list = []
    # for data in data_list:
    #     title = data.get("title")
    #     other_title = data.get("other_title")
    #     score = data.get("rate")
    #     image = data.get("cover")
    #     order = i
    #     desc = data.get("description")
    #     datePublished = data.get("datePublished").split("-")
    #     star = data.get("star")
    #     obj_list.append(models.Video(
    #         title= title,
    #         other_title= other_title,
    #         score = score,
    #         image=image,
    #         order=order,
    #         desc=desc,
    #         pub_time=datetime.date(
    #             year=int(datePublished[0]),
    #             month=int(datePublished[1]),
    #             day=int(datePublished[2])),
    #         star = star,
    #         category_id = random.choice(range(1,7)),
    #         region_id = random.choice(range(1,22)),
    #         period_id = random.choice(range(1,10)),
    #     ))
    #     i += 1
    # obj_list = models.Video.objects.bulk_create(obj_list)
    # for i in range(len(obj_list)):
    #     directors = data_list[i].get("directors")  # 导演
    #     casts = data_list[i].get("casts")  # 演员
    #     authors = data_list[i].get("authors")  # 作者
    #     director_obj = models.Role.objects.filter(name="导演").first()
    #     cast_obj = models.Role.objects.filter(name="演员").first()
    #     author_obj = models.Role.objects.filter(name="作者").first()
    #
    #     for name in directors:
    #         # 演职员
    #         obj = models.Credits.objects.filter(name=name).first()
    #         if obj:
    #             models.Video_Credits.objects.create(
    #                 video_id=i+1,
    #                 credits=obj,
    #                 role=director_obj
    #             )
    #     for name in casts:
    #         # 演职员
    #         obj = models.Credits.objects.filter(name=name).first()
    #         if obj:
    #             models.Video_Credits.objects.create(
    #                 video_id=i+1,
    #                 credits=obj,
    #                 role=cast_obj
    #             )
    #     for name in authors:
    #         # 演职员
    #         obj = models.Credits.objects.filter(name=name).first()
    #         if obj:
    #             models.Video_Credits.objects.create(
    #                 video_id=i+1,
    #                 credits=obj,
    #                 role=author_obj
    #             )
    # obj_list = models.Video.objects.all()
    # for obj in obj_list:
    #     r1, r2 = random.sample(Type_list, 2)
    #     obj.type.add(r1,r2)

    '''插入演员数据'''
    # Credits_list = []
    #
    # with open(f"../vedioapi/media/处理后的豆瓣电影数据.json", "r", encoding="utf8") as f:
    #     data_dic = json.load(f)
    # data_list = data_dic["data"]
    # for data in data_list:
    #     [Credits_list.append(name) for name in data.get("directors")]
    #     [Credits_list.append(name) for name in data.get("casts")]
    #     [Credits_list.append(name) for name in data.get("authors")]
    #
    # with open(f"../vedioapi/media/处理后的豆瓣电视剧数据.json", "r", encoding="utf8") as f:
    #     data_dic = json.load(f)
    # data_list = data_dic["data"]
    # for data in data_list:
    #     [Credits_list.append(name) for name in data.get("directors")]
    #     [Credits_list.append(name) for name in data.get("casts")]
    #     [Credits_list.append(name) for name in data.get("authors")]
    #
    #
    # print(Credits_list,len(Credits_list))
    # Credits_list = set(Credits_list)
    # print(Credits_list,len(Credits_list))
    #
    # obj_list = []
    # for name in Credits_list:
    #     obj_list.append(models.Credits(name=name, gender=random.choice((0,1))))
    # models.Credits.objects.bulk_create(obj_list)

    '''插入类型，地区，年代表数据'''
    # Type_list = ["经典", "青春", "文艺", "搞笑", "励志", "魔幻", "感人", "女性", "黑帮"]
    #
    # Period_list = ["2019","2018","2010年代","2000年代","90年代","80年代","70年代","60年代","更早"
    #               ]
    #
    # Region_list = ["中国大陆",
    #               "美国",
    #               "中国香港",
    #               "中国台湾",
    #               "日本",
    #               "韩国",
    #               "英国",
    #               "法国",
    #               "德国",
    #               "意大利",
    #               "西班牙",
    #               "印度",
    #               "泰国",
    #               "俄罗斯",
    #               "伊朗",
    #               "加拿大",
    #               "澳大利亚",
    #               "爱尔兰",
    #               "瑞典",
    #               "巴西",
    #               "丹麦",
    #               ]
    # obj_list = []
    #
    #
    # for name in Region_dic:
    #     obj_list.append(models.Region(name=name))
    #
    # models.Region.objects.bulk_create(obj_list)
